<!-- 商城 - sku组件 -->
<template>
	<vk-data-goods-sku-popup
		ref="skuPopup"
		:value="value"
		border-radius="20"
		:localdata="goodsInfo"
		:mode="modeCom"
		:selected-init="false"
		theme="red-black"
		priceColor="var(--main)"
		buyNowColor="#ffffff"
		buyNowBackgroundColor="var(--main)"
		addCartColor="#ffffff"
		addCartBackgroundColor="#606060"
		:activedStyle="{
			color: 'var(--main)',
			borderColor: 'var(--main)',
			backgroundColor: 'rgba(255,68,68,0.1)'
		}"
		:minBuyNum="cartRuleCom.min"
		:maxBuyNum="cartRuleCom.max"
		:stepBuyNum="cartRuleCom.step"
		:stepStrictly="cartRuleCom.step_strictly"
		@input="_input"
		@open="onOpenSkuPopup"
		@close="onCloseSkuPopup"
		@cart="addCart"
		@buy="buyNow"
	></vk-data-goods-sku-popup>
</template>

<script>
export default {
	name: "vk-mall-goods-sku-popup",
	props: {
		value:{

		},
		goodsInfo:{
			Type:Object
		},
		mode:{
			Type:Number,
			default:1
		}
	},
	data: function() {
		// 组件创建时,进行数据初始化
		return {
			data:{

			}
		};
	},
	mounted() {
		this.init();
	},
	methods: {
		// 初始化
		init() {},
		click(item){
			this.$emit("click",item)
		},
		_input(value){
			this.$emit("input",value);
		},
		open(){
			this._input(true);
		},
		close(){
			this._input(false);
		},
		// sku组件 开始-----------------------------------------------------------
		onOpenSkuPopup() {

		},
		onCloseSkuPopup() {

		},
		// 加入购物车前的判断
		addCartFn(obj) {
			let that = this;
			let { vk } = that;
			let { selectShop } = obj;
			// 模拟添加到购物车,请替换成你自己的添加到购物车逻辑
			let res = {};
			let name = selectShop.goods_name;
			if (selectShop.sku_name != "默认") {
				name += "-" + selectShop.sku_name_arr;
			}
			res.msg = `${name} * ${selectShop.buy_num} 已添加到购物车`;
			if (typeof obj.success == "function") obj.success(res);
		},
		// 加入购物车按钮
		addCart(selectShop) {
			let that = this;
			let { vk, goodsInfo } = that;
			//console.log("监听 - 加入购物车", selectShop);
			that.addCartFn({
				selectShop: selectShop,
				success: (res) => {
					let addCartRes = vk.myfn.cart.add({ goodsInfo, selectShop });
					vk.toast(addCartRes.msg);
					setTimeout(() => {
						that.close();
					}, 300);
				}
			});
		},
		// 立即购买
		buyNow(selectShop) {
			let that = this;
			let { vk } = that;
			//console.log("监听 - 立即购买", selectShop);
			that.addCartFn({
				selectShop: selectShop,
				success: (res) => {
					vk.navigateTo(`/pages/goods/goods-order-confirm?mode=buy&sku_id=${selectShop._id}&goods_id=${selectShop.goods_id}&buy_num=${selectShop.buy_num}`);
				}
			});
		},
	},
	// 监听器
	watch:{

	},
	// 计算属性
	computed: {
		modeCom(){
			let { mode, goodsInfo } = this;
			if (goodsInfo.type === 1) {
				return 3;
			}
			return mode;
		},
		cartRuleCom(){
			let { goodsInfo={} } = this;
			let { cart_rule={}, limit_buy_rule={} } = goodsInfo;
			let {
				max = 100000,
				min = 1,
				step = 1,
				step_strictly = false
			} = cart_rule;
			let {
				activity_limit_num,
				every_day_limit_num,
			} = limit_buy_rule;
			if (activity_limit_num > 0 && max > activity_limit_num) {
				max = activity_limit_num;
			}
			if (every_day_limit_num > 0 && max > every_day_limit_num) {
				max = every_day_limit_num;
			}
			return {
				max,
				min,
				step,
				step_strictly
			}
		}
	}
};
</script>

<style lang="scss" scoped>

</style>
